package com.goldgov.addressbook.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.gtiles.components.securityworkbench.bean.SwbAuthUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.goldgov.addressbook.service.AddressBookGroup;
import com.goldgov.addressbook.service.AddressBookGroupQuery;
import com.goldgov.addressbook.service.AddressBookGroupService;
import com.goldgov.addressbook.service.AddressBookGroupUser;
import com.goldgov.addressbook.service.AddressBookGroupUserQuery;
import com.goldgov.addressbook.service.AddressBookGroupUserService;
import com.goldgov.addressbook.service.AddressUser;
import com.goldgov.addressbook.service.AddressUserQuery;
import com.goldgov.conference.service.ConferenceUser;
import com.goldgov.kcloud.core.json.JsonObject;
import com.goldgov.kcloud.core.json.JsonQueryObject;
import com.goldgov.kcloud.core.json.JsonSuccessObject;
import com.goldgov.login.web.model.LoginAuthUser;
import com.goldgov.utils.JsonUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

/** 
 * @ClassName: AddressBookGroupController 
 * @Description: TODO
 * @author MaN
 * @date 2018年7月16日 
 */

@RestController
@RequestMapping("/workbench/addressbook")
@Api("通讯录接口")
public class AddressBookGroupController {

	@Autowired 
	private AddressBookGroupService addressBookGroupService;
	
	@Autowired 
	private AddressBookGroupUserService addressBookGroupUserService;
	
	@PostMapping
	@ApiOperation(value = "新增通讯录群组", notes ="")
	public JsonObject<Object> addAddressBookGroup(AddressBookGroup obj,HttpServletRequest request){
		SwbAuthUser u=LoginAuthUser.getUserByAdmin(request);
		obj.setUserID(u.getEntityID());//发布人id
		obj.setUserName(u.getUserName());
		obj.setLastModifyTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		obj.setActiveState(AddressBookGroupQuery.ACTIVESTATE_YES);
		addressBookGroupService.addAddressBookGroup(obj);
		return JsonSuccessObject.SUCCESS;
	}
	
	@GetMapping("/listAddressBookGroup")
	@ApiOperation(value = "通讯录群组列表", notes ="")
	public JsonQueryObject<AddressBookGroup> listAddressBookGroup(@ApiIgnore AddressBookGroupQuery query,HttpServletRequest request){
		SwbAuthUser u=LoginAuthUser.getUserByAdmin(request);
		query.setSearchUserID(u.getEntityID());
		List<AddressBookGroup> list=addressBookGroupService.getAddressBookGroupList(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@PostMapping("/updateAddressBookGroup")
	@ApiOperation(value = "修改通讯录群组", notes ="")
	public JsonObject<Object> updateAddressBookGroup(AddressBookGroup obj,HttpServletRequest request){
		obj.setLastModifyTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		addressBookGroupService.updateAddressBookGroup(obj);
		return JsonSuccessObject.SUCCESS;
	}
	@GetMapping("/deleteAddressBookGroup")
	@ApiOperation(value = "删除通讯录群组", notes = "")
	public JsonObject<Object> deleteAddressBookGroup(AddressBookGroup obj,HttpServletRequest request,HttpServletResponse response) throws Exception {
		AddressBookGroupUserQuery query=new AddressBookGroupUserQuery();
		query.setSearchGroupID(obj.getGroupID());
		List<AddressBookGroupUser> list=addressBookGroupUserService.getAddressBookGroupUserList(query);
		if(null!=list && list.size()>0) {
			addressBookGroupUserService.deleteAddressBookGroupUser(obj.getGroupID());
		}
		obj.setActiveState(AddressBookGroupQuery.ACTIVESTATE_NO);
		addressBookGroupService.updateAddressBookGroup(obj);
		return JsonSuccessObject.SUCCESS;
	}
	@PostMapping("/saveAddressBookGroupUser")
	@ApiOperation(value = "编辑通讯录群组人员", notes ="")
	public JsonObject<Object> addAddressBookGroupUser(AddressBookGroupUser obj,HttpServletRequest request){
		String groupID=obj.getGroupID();
		AddressBookGroupUserQuery query=new AddressBookGroupUserQuery();
		query.setSearchGroupID(groupID);
		List<AddressBookGroupUser> list=addressBookGroupUserService.getAddressBookGroupUserList(query);
		if(null!=list && list.size()>0) {
			addressBookGroupUserService.deleteAddressBookGroupUser(groupID);
		}
        String[] userIDs=obj.getUserIDs();
        if(null!=userIDs && userIDs.length>0) {
        	for(String userID:userIDs) {
        		AddressBookGroupUser user=new AddressBookGroupUser();
        		user.setGroupID(groupID);
        		user.setUserID(userID);
        		addressBookGroupUserService.addAddressBookGroupUser(user);
        	}
        }
		return JsonSuccessObject.SUCCESS;
	}
	@GetMapping("/listAddressBookGroupUser")
	@ApiOperation(value = "通讯录群组人员列表", notes ="")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchGroupID", value = "群组id", paramType = "query")})
	public JsonQueryObject<AddressBookGroupUser> listAddressBookGroupUser(@ApiIgnore AddressBookGroupUserQuery query,HttpServletRequest request){
		List<AddressBookGroupUser> list=addressBookGroupUserService.getAddressBookGroupUserList(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	@GetMapping("/listOrgUser")
	@ApiOperation(value = "机构人员列表", notes ="")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchOrgID", value = "机构id", paramType = "query")})
	public JsonQueryObject<AddressBookGroupUser> listOrgUser(@ApiIgnore AddressBookGroupUserQuery query,HttpServletRequest request){
		List<AddressBookGroupUser> list=addressBookGroupUserService.getOrgUserList(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	
	@RequestMapping("/listUser")
	@ApiOperation(value = "通讯录人员列表", notes ="")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchClassifyID", value = "分组id", paramType = "query"),
		@ApiImplicitParam(name = "searchName", value = "人员姓名", paramType = "query")})
	public JsonQueryObject<AddressUser> listUser(@ApiIgnore AddressUserQuery query,HttpServletRequest request){
		List<AddressUser> list=addressBookGroupUserService.findAddressUser(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@DeleteMapping("/deleteUser")
	@ApiOperation(value = "删除通讯录人员", notes ="")
	public JsonObject<Object> deleteUser(@ApiParam(value = "userIDs", required = true) @RequestParam("ids") String[] ids,HttpServletResponse response){
		if (addressBookGroupUserService.deleteAddressUser(ids)) {
			return JsonUtil.success("删除成功!");
		}else {
			return JsonUtil.error("系统出错,请联系管理员!");
		}
	}
	@PostMapping("/addUser")
	@ApiOperation(value = "添加通讯录人员", notes ="")
	public JsonObject<Object> addUser(AddressUser addressUser){
		if (addressBookGroupUserService.addAddressUser(addressUser)) {
			return JsonUtil.success("添加成功!");
		}else {
			return JsonUtil.error("系统出错,请联系管理员!");
		}
	}
	
	@GetMapping("/existUserIds")
	@ApiOperation(value = "获取已选人ids", notes ="")
	public JsonSuccessObject<List<String>> getConferenceUserIds(String classifyID,HttpServletRequest request){
		List<String> ids = addressBookGroupUserService.getExistUserIds(classifyID);
		JsonSuccessObject<List<String>> returnResult = new JsonSuccessObject<List<String>>(ids);
		return returnResult;
	}
	
	
	
}
